﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace lcs.Web.monthsked
{
    public partial class ModifyCJ : Web.Admin.BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int mid = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(mid);
                    BLL.monthruleinfo bll = new BLL.monthruleinfo();

                    DataSet ds = bll.GetList("mid=" + mid);
                    gvrule.DataSource = ds.Tables[0];
                    gvrule.DataBind();
                }
            }
        }

        private void ShowInfo(int mid)
        {
            lcs.BLL.monthsked bll = new lcs.BLL.monthsked();
            DataSet ds = bll.GetList(" and mid=" + mid);
            if (ds.Tables[0].Rows.Count == 1)
            {
                this.lblspotunit.Text = ds.Tables[0].Rows[0]["spotunit"].ToString();
                this.lblprojectname.Text = ds.Tables[0].Rows[0]["projectname"].ToString();
                this.lblsname.Text = ds.Tables[0].Rows[0]["sname"].ToString();
                this.lblname.Text = ds.Tables[0].Rows[0]["material"].ToString();
                this.lblspottype.Text = ds.Tables[0].Rows[0]["spottype"].ToString();
                this.lblspotnum.Text = ds.Tables[0].Rows[0]["spotnum"].ToString();
                hfsid.Value = ds.Tables[0].Rows[0]["sid"].ToString();
                this.lblspotcontent.Text = ds.Tables[0].Rows[0]["spotcontent"].ToString();
                this.lblusework.Text = ds.Tables[0].Rows[0]["usework"].ToString();
                //this.txtspotopinion.Text = ds.Tables[0].Rows[0]["spotopinion"].ToString();
                this.txtquestion.Text = ds.Tables[0].Rows[0]["question"].ToString();
                this.ddlresult.SelectedValue = ds.Tables[0].Rows[0]["qresult"].ToString();
            }

        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int mid = (Convert.ToInt32(Request.Params["id"]));
                string spotopinion = "";
                string qresult = this.ddlresult.Text;
                string question = this.txtquestion.Text;
                int uid = getid();
                int sid = 0;
                int.TryParse(hfsid.Value, out sid);
                string idlist = "";
                bool BxsChkd = false;
                int mrid = 0;
                Model.monthruleinfo modelm = new Model.monthruleinfo();
                BLL.monthruleinfo bllm = new BLL.monthruleinfo();
                modelm.opid = uid;
                int numr = 0;
                int numw = 0;
                int cnum = 0;

                for (int i = 0; i < gvrule.Rows.Count; i++)
                {
                    CheckBox ChkBxr = (CheckBox)gvrule.Rows[i].FindControl("rbr");
                    CheckBox ChkBxw = (CheckBox)gvrule.Rows[i].FindControl("rbw");
                    if (gvrule.DataKeys[i].Value != null)
                    {
                        int.TryParse(gvrule.DataKeys[i].Value.ToString(), out mrid);
                        modelm.mrid = mrid;
                        modelm.isrw = !ChkBxr.Checked;
                        modelm.opdatetime = DateTime.Now;
                        bllm.UpdateOP(modelm);
                        if (spotopinion != "") spotopinion += "<br />";
                        spotopinion += gvrule.Rows[i].Cells[0].Text + gvrule.Rows[i].Cells[1].Text;
                        if (ChkBxr != null && ChkBxr.Checked)
                        {
                            int.TryParse(ChkBxr.Text, out cnum);
                            numr += cnum;
                            spotopinion += "正" + cnum;
                        }
                        else if (ChkBxw != null && ChkBxw.Checked)
                        {
                            int.TryParse(ChkBxw.Text, out cnum);
                            numw += cnum;
                            spotopinion += "负" + cnum;
                        }


                    }
                }
                BLL.supplierinfo sbll = new BLL.supplierinfo();
                if (numw != 0)
                {
                    sbll.UpdateSum(sid, numw);

                }
                else
                {
                    sbll.UpdateSum(sid, numr);
                }
                if (BxsChkd)
                {
                    idlist = idlist.Substring(0, idlist.LastIndexOf(","));
                }
                lcs.BLL.monthsked bll = new lcs.BLL.monthsked();
                lcs.Model.monthsked model = bll.GetModel(mid);
                //model.mid = mid;
                model.spotopinion = spotopinion;
                model.qresult = qresult;
                model.question = question;                
                bll.Updateresult(model);
                bll.Updatestatus(model.mid, 7);
                BLL.gradeinfo bllg = new BLL.gradeinfo();
                Model.gradeinfo modelg = new Model.gradeinfo();
                modelg.monthsupplyid = model.monthsupplyid;
                decimal ss = 0, ee = 0;
                ss = model.msnum.Value;
                modelg.shs = ss;
                modelg.dhs =ss;

                if (qresult == "合格")
                    ee = ss;
                modelg.she = ee;
                modelg.dhe = ee;
                //IF(Q7 / P7 >= 0.95, 5 - (1 - Q7 / P7) * 100, 0)

                decimal jg = 0, dh = 0;
                if(ee!=0)
                { 
                jg = ss / ee;
                if (jg > 0.95M)
                {
                    jg = 5 - (1 - jg) * 100;
                }
                else
                    jg = 0;
                }
                else
                { 
                jg = 0;
                }
                if (ee != 0)
                {
                    dh = ss / ee;
                    if (dh > 0.975M)
                    {
                        dh = 5 - (1 - dh) * 200;
                    }
                    else
                        dh = 0;
                }
                else
                {
                    dh = 0;
                }
                modelg.shmark = jg;
                modelg.dhmark = dh;
                bllg.UpdateYS(modelg);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "MyList.aspx");
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyList.aspx");
        }
    }
}
